#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "friend.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(friend_service, "/service/friend_service", "服务监控根目录");

mag::ServiceManager::ptr sm;

void apply_test(const std::string &uid1, const std::string &uid2) {
    std::cout << "开始申请添加好友，uid1: " << uid1 << ", uid2: " << uid2 << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::FriendAddReq req;
    mag::FriendAddRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    req.set_respondent_id(uid2);
    brpc::Controller cntl;
    std::cout << "发送添加好友请求..." << std::endl;
    stub.FriendAdd(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "申请添加好友成功!" << std::endl;
}

void get_apply_list(const std::string &uid1) {
    std::cout << "获取申请列表，uid1: " << uid1 << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::GetPendingFriendEventListReq req;
    mag::GetPendingFriendEventListRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    brpc::Controller cntl;
    std::cout << "发送获取申请列表请求..." << std::endl;
    stub.GetPendingFriendEventList(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "获取申请列表成功，数量: " << rsp.event_size() << std::endl;
    for (int i = 0; i < rsp.event_size(); i++) {
        std::cout << "---------------\n";
        std::cout << "申请人ID: " << rsp.event(i).sender().user_id() << std::endl;
        std::cout << "申请人昵称: " << rsp.event(i).sender().nickname() << std::endl;
        std::cout << "申请人头像: " << rsp.event(i).sender().avatar() << std::endl;
    }
}

void process_apply_test(const std::string &uid1, bool agree, const std::string &apply_user_id) {
    std::cout << "处理申请，uid1: " << uid1 << ", 同意: " << (agree ? "是" : "否") << ", 申请人: " << apply_user_id << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::FriendAddProcessReq req;
    mag::FriendAddProcessRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    req.set_agree(agree);
    req.set_apply_user_id(apply_user_id);
    brpc::Controller cntl;
    std::cout << "发送处理申请请求..." << std::endl;
    stub.FriendAddProcess(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    if (agree) {
        std::cout << "同意添加好友，新的会话ID: " << rsp.new_session_id() << std::endl;
    }
    std::cout << "申请处理成功!" << std::endl;
}

void search_test(const std::string &uid1, const std::string &key) {
    std::cout << "搜索好友，uid1: " << uid1 << ", 搜索关键字: " << key << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::FriendSearchReq req;
    mag::FriendSearchRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    req.set_search_key(key);
    brpc::Controller cntl;
    std::cout << "发送好友搜索请求..." << std::endl;
    stub.FriendSearch(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "搜索结果成功，找到 " << rsp.user_info_size() << " 条匹配项。" << std::endl;
    for (int i = 0; i < rsp.user_info_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << "用户ID: " << rsp.user_info(i).user_id() << std::endl;
        std::cout << "昵称: " << rsp.user_info(i).nickname() << std::endl;
        std::cout << "头像: " << rsp.user_info(i).avatar() << std::endl;
    }
}

void friend_list_test(const std::string &uid1) {
    std::cout << "获取好友列表，uid1: " << uid1 << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::GetFriendListReq req;
    mag::GetFriendListRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    brpc::Controller cntl;
    std::cout << "发送获取好友列表请求..." << std::endl;
    stub.GetFriendList(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "获取好友列表成功，数量: " << rsp.friend_list_size() << std::endl;
    for (int i = 0; i < rsp.friend_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << "好友ID: " << rsp.friend_list(i).user_id() << std::endl;
        std::cout << "好友昵称: " << rsp.friend_list(i).nickname() << std::endl;
        std::cout << "好友头像: " << rsp.friend_list(i).avatar() << std::endl;
    }
}

void remove_test(const std::string &uid1, const std::string &uid2) {
    std::cout << "移除好友，uid1: " << uid1 << ", uid2: " << uid2 << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::FriendRemoveReq req;
    mag::FriendRemoveRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    req.set_peer_id(uid2);
    brpc::Controller cntl;
    std::cout << "发送移除好友请求..." << std::endl;
    stub.FriendRemove(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "移除好友成功!" << std::endl;
}

void create_css_test(const std::string &uid1, const std::vector<std::string> &uidlist) {
    std::cout << "创建聊天会话，uid1: " << uid1 << ", 会话成员: ";
    for (const auto &id : uidlist) {
        std::cout << id << " ";
    }
    std::cout << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::ChatSessionCreateReq req;
    mag::ChatSessionCreateRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    req.set_chat_session_name("快乐一家人");
    for (auto &id : uidlist) {
        req.add_member_id_list(id);
    }
    brpc::Controller cntl;
    std::cout << "发送创建聊天会话请求..." << std::endl;
    stub.ChatSessionCreate(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "创建聊天会话成功, 会话ID: " << rsp.chat_session_info().chat_session_id() << std::endl;
}

void cssmember_test(const std::string &uid1, const std::string &cssid) {
    std::cout << "获取聊天会话成员，uid1: " << uid1 << ", 会话ID: " << cssid << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::GetChatSessionMemberReq req;
    mag::GetChatSessionMemberRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    req.set_chat_session_id(cssid);
    brpc::Controller cntl;
    std::cout << "发送获取聊天会话成员请求..." << std::endl;
    stub.GetChatSessionMember(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "获取成员列表成功，数量: " << rsp.member_info_list_size() << std::endl;
    for (int i = 0; i < rsp.member_info_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << "成员ID: " << rsp.member_info_list(i).user_id() << std::endl;
        std::cout << "成员昵称: " << rsp.member_info_list(i).nickname() << std::endl;
        std::cout << "成员头像: " << rsp.member_info_list(i).avatar() << std::endl;
    }
}

void csslist_test(const std::string &uid1) {
    std::cout << "获取聊天会话列表，uid1: " << uid1 << std::endl;
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    mag::FriendService_Stub stub(channel.get());
    mag::GetChatSessionListReq req;
    mag::GetChatSessionListRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_user_id(uid1);
    brpc::Controller cntl;
    std::cout << "发送获取聊天会话列表请求..." << std::endl;
    stub.GetChatSessionList(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << "获取聊天会话列表成功，数量: " << rsp.chat_session_info_list_size() << std::endl;
    for (int i = 0; i < rsp.chat_session_info_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << "好友ID: " << rsp.chat_session_info_list(i).single_chat_friend_id() << std::endl;
        std::cout << "会话ID: " << rsp.chat_session_info_list(i).chat_session_id() << std::endl;
        std::cout << "会话名: " << rsp.chat_session_info_list(i).chat_session_name() << std::endl;
        std::cout << "头像: " << rsp.chat_session_info_list(i).avatar() << std::endl;
        std::cout << "消息内容: " << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().message_id() << std::endl;
    }
}


int main(int argc , char*argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    sm = std::make_shared<mag::ServiceManager>();
    sm->declared(FLAGS_friend_service);
    auto put_cb = std::bind(&mag::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&mag::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    //2. 构造服务发现对象
    mag::Discovery::ptr dclient = std::make_shared<mag::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    // 1. 发送好友申请
    // apply_test("bd47-3a63f889-0000", "8d25-14d9ff25-0000");
    // apply_test("78e1-166ca727-0000", "8d25-14d9ff25-0000");
    // apply_test("e1f1-73325bb6-0000", "8d25-14d9ff25-0000");

    // 2. 获取好友申请列表
    // get_apply_list("8d25-14d9ff25-0000");

    // std::cout << "**********************\n";

    // // 3. 处理好友申请
    // process_apply_test("8d25-14d9ff25-0000", true, "bd47-3a63f889-0000");  // 同意
    // process_apply_test("8d25-14d9ff25-0000", false,"78e1-166ca727-0000"); // 拒绝
    // process_apply_test("8d25-14d9ff25-0000", true, "e1f1-73325bb6-0000");  // 同意

    // get_apply_list("8d25-14d9ff25-0000");

    std::cout << "**********************\n";

    // 4. 搜索好友
    // search_test("8d25-14d9ff25-0000", "猪");
    // std::cout << "++++++++++++++++++++++\n";
    // search_test("bd47-3a63f889-0000", "猪");
    // std::cout << "======================\n";
    // search_test("78e1-166ca727-0000", "猪");

    // 5. 获取好友列表
    // friend_list_test("bd47-3a63f889-0000");
    // std::cout << "++++++++++++++++++++++\n";
    // friend_list_test("78e1-166ca727-0000");
    // std::cout << "++++++++++++++++++++++\n";
    // friend_list_test("e1f1-73325bb6-0000");

    // // 6. 移除好友（佩奇删除猪小弟）
    // remove_test("8d25-14d9ff25-0000", "e1f1-73325bb6-0000");

    // 7. 创建聊天会话
    std::vector<std::string> uidlist = {
        "bd47-3a63f889-0000", 
        "8d25-14d9ff25-0000",
            };
    create_css_test("731f-50086884-0000", uidlist);

    // 8. 获取聊天会话成员
    cssmember_test("731f-50086884-0000", "36b5-edaf4987-0000");
    std::cout << "++++++++++++++++++++++\n";
    cssmember_test("8d25-14d9ff25-0000", "36b5-edaf4987-0000");

    // 9. 获取聊天会话列表
    csslist_test("8d25-14d9ff25-0000");



    return 0;
}
